﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using FxCopContrib.TestHarness;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FxCopContrib.Tests
{
    [TestClass]
    public class ConsiderMakingRegexReadOnlyAndCompiledTests
    {
        public TestContext TestContext { get; set; }

        [TestMethod]
        public void ARegexObjectThatIsInitializedWithAStaticStringShouldTriggerReadOnlyAndStaticFlag()
        {
            FxCopTest
                .ForRule<ConsiderMakingRegexReadOnlyAndCompiled>()
                .OnMethod(
                    @"
                        Regex rx = new Regex(""[a-z]+"");
                    ",
                    typeof(Regex).Namespace
                )
                .WithReferencesTo(typeof(Regex))
                .WillFind(
                    ProblemPrototype.WithResulutionName("UseStaticField"), 
                    ProblemPrototype.WithResulutionName("UseReadonlyField")
                )
                .Conclusive()
                .Verify();
        }

        [TestMethod]
        public void ARegexObjectThatIsInitializedWithAStaticStringShouldTriggerReadOnlyFlag()
        {
            FxCopTest
                .ForRule<ConsiderMakingRegexReadOnlyAndCompiled>()
                .OnMethod(@"
                        static Regex rx = new Regex(""[a-z]+"");
                    ", typeof(Regex).Namespace
                )
                .WithReferencesTo(typeof(Regex))
                .WillFind(ProblemPrototype.WithResulutionName("UseReadonlyField"))
                .Conclusive()
                .Verify();
        }

        [TestMethod]
        public void ARegexObjectThatIsInitializedWithAStaticStringShouldTriggerStaticFlag()
        {
            FxCopTest
                .ForRule<ConsiderMakingRegexReadOnlyAndCompiled>()
                .OnMethod(@"
                    readonly Regex rx = new Regex(""[a-z]+"");
                ", typeof(Regex).Namespace
                )
                .WithReferencesTo(typeof(Regex))
                .WillFind(ProblemPrototype.WithResulutionName("UseStaticField"))
                .Conclusive()
                .Verify();
        }

        [TestMethod]
        public void AStaticReadonlyRegexObjectThatIsInitializedWithAStaticStringShouldTriggerCompiledFlag()
        {
            FxCopTest
                .ForRule<ConsiderMakingRegexReadOnlyAndCompiled>()
                .OnMethod(
                    @"
                        static readonly Regex rx = new Regex(""[a-z]+"");
                    ",
                    typeof(Regex).Namespace
                )
                .WithReferencesTo(typeof(Regex))
                .WillFind(ProblemPrototype.WithResulutionName("UseRegexOptionsCompiled"))
                .Conclusive()
                .Verify();
        }

        [TestMethod]
        public void CorrectUsageShouldNotTriggerRule()
        {
            FxCopTest
                .ForRule<ConsiderMakingRegexReadOnlyAndCompiled>()
                .OnMethod(
                    @"
                        static readonly Regex rx = new Regex(""[a-z]+"", RegexOptions.Compiled);
                    ",
                    typeof(Regex).Namespace
                )
                .WithReferencesTo(typeof(Regex), typeof(RegexOptions))
                .WillNotFind(ProblemPrototype.Default)
                .Conclusive()
                .Verify();
        }
    }
}
